$(function () {
    $.ajax({
        type: 'POST',
        url: '/enable/getOppenMerchantMsg',
        async: false,
        data: {},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                var merchantCount = result.data.merchantList.length;
                var count = 0;
                for (i = 0;i<merchantCount;i++) {
                    $("#able-merchant").append("<div class='able-a-merchant'><a class='able-a-merchant ' onclick='settleDetail("+result.data.merchantList[i].id+");'>"+result.data.merchantList[i].name+"</a></div>");
                }
                settleDetail(result.data.merchantList[0].id);
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });
});
function settleDetail(id) {
    $.ajax({
        type: 'POST',
        url: '/settlement/settleDetail',
        async: false,
        data: {"merchantId":id},
        dataType: 'json',
        success: function (result) {
            console.log(result.data);
            $("#print-content").empty();
            if (result.code==666){
                $("#merchantId").val(id);
                $("#headName").text(result.data.merchant.name);
                if (result.data.merchant.state == 1) {
                    $("#state").text("启用");
                }else {
                    $("#state").text("未启用");
                }
                $("#code").text(result.data.merchant.code);
                var accountlength = result.data.accountList.length
                var costCount=0;
                var balance=result.data.merchant.balance;
                $("#print-content").append(" <tr height=\"45\" style=\"text-align: center;font-size:14px\">\n" +
                    "                                    <td colspan=\"2\" class=\"on\">计费项目</td>\n" +
                    "                                    <td colspan=\"2\" class=\"on\">前次读数</td>\n" +
                    "                                    <td colspan=\"2\" class=\"on\">今次读数</td>\n" +
                    "                                    <td colspan=\"2\" class=\"on\">实用</td>\n" +
                    "                                    <td colspan=\"2\" class=\"on\">计费金额</td>\n" +
                    "                                  </tr>");
                if (accountlength>0){
                    for (var i=0;i<accountlength;i++){
                            $("#print-content").append("  <tr height=\"45\" style=\"text-align: center;font-size:14px\" >\n" +
                                "                                    <td colspan=\"2\" >"+result.data.accountList[i].name+"</td>\n" +
                                "                                    <td colspan=\"2\"  >"+result.data.accountList[i].beforereading+"</td>\n" +
                                "                                    <td colspan=\"2\"  >"+result.data.accountList[i].nowreading+"</td>\n" +
                                "                                    <td colspan=\"2\" >"+result.data.accountList[i].usage+"</td>\n" +
                                "                                    <td colspan=\"2\" >"+result.data.accountList[i].cost+"</td>\n" +
                                "                                </tr>")
                        costCount+=result.data.accountList[i].cost;
                    }
                }
                $("#print-content").append(" <tr height=\"45\" style=\"text-align: center;font-size:14px\">\n" +
                    "                                    <td colspan=\"2\" class=\"on\">总计</td>\n" +
                    "                                    <td colspan=\"4\" >"+costCount+"</td>\n" +
                    "                                    <td colspan=\"2\" class=\"on\">待缴金额</td>\n" +
                    "                                    <td colspan=\"2\">"+costCount+"</td>\n" +
                    "                                </tr>\n" +
                    "                                <tr height=\"45\" style=\"text-align: center;font-size:14px\">\n" +
                    "                                    <td colspan=\"4\" class=\"on\">余额</td>\n" +
                    "                                    <td colspan=\"6\" >"+balance+"</td>\n" +
                    "                                 </tr>");
                if (balance-costCount>0){
                    $("#return").text(balance-costCount);
                    $("#debt").text("0.00");
                }else {
                    $("#return").text("0.00");
                    $("#debt").text(balance-costCount);
                }
                $("#comname").text(result.data.merchant.name);
                $("#linkman").text(result.data.merchant.phone1);
                $("#time").text(formatDate(Date.parse(new Date())/1000));
                $("#settlePerson").text(result.data.companyName);
                $("#cost").text(costCount);
                $("#beforesettle").text(balance);
                $("#aftersettle").text(balance-costCount);
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });
}
$("#settlement-btn").click(function () {
    var merchantId = $("#merchantId").val();
        var layer;
        layui.use(['layer', 'form'],function() {
        layer = layui.layer;
        layer.confirm("您将对该公司进行结算是否确认？", {
            btn: ['确定', '取消'] //按钮
        }, function () {
            layer.close(layer.index);
            var pwd = $("#pwd").val();
            $.ajax({
                type: 'POST',
                url: '/settlement/checkSettlePwd',
                async: false,
                data: {"pwd": pwd,"merchantId":merchantId},
                dataType: 'json',
                success: function (result) {
                    if (result.code == 666) {
                        $('.re').css('display', 'none');
                        $('.re2').css('display', 'block');
                    } else {
                        var layer;
                        layui.use(['layer', 'form'], function () {
                            layer = layui.layer
                            layer.alert('密码错误', {icon: 2}, function (index) {
                                //do something
                                layer.close(index);
                            });

                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (XMLHttpRequest.status==555){
                        var layer;
                        layui.use(['layer', 'form'], function(){
                            layer = layui.layer
                            layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                                //do something
                                layer.close(index);
                                top.location.href="/login";
                            });
                        });
                    }else{
                        var layer;
                        layui.use(['layer', 'form'], function(){
                            layer = layui.layer
                            layer.alert('页面加载失败',{icon:7}, function(index){
                                //do something
                                layer.close(index);
                            });
                        });
                    }
                },
            });
        }, function () {
        });
    });

});
function formatDate(timestamp) {
    if (timestamp) {
        var time = new Date(timestamp*1000);
        var y = time.getFullYear();
        var M = time.getMonth() + 1;
        var d = time.getDate();
        var h = time.getHours();
        var m = time.getMinutes();
        var s = time.getSeconds();
        return y + '-' + addZero(M) + '-' + addZero(d) + ' ' + addZero(h) + ':' + addZero(m) + ':' + addZero(s);
    } else {
        return '';
    }
}
function addZero(m) {
    return m < 10 ? '0' + m : m;
}
$('#ad').click(function () {
    window.location.reload()
});
//表格导出
function exportTable() {
    // 获取头部和body
    var data = LAY_EXCEL.tableToJson(document.getElementById('print-content')) // 或者 $('#LAY-EXPORT-TEST')
    // console.log(data)
    var exportData = []
    exportData.push.apply(exportData, data.head)
    exportData.push.apply(exportData, data.body)
    // console.log(exportData)

    LAY_EXCEL.exportExcel(exportData, '结算收据.xlsx', 'xlsx')
}
//打印
function printout() {
    debugger;
    var newWindow;
    //打开一个新的窗口
    newWindow = window.open();
    // 是新窗口获得焦点
    newWindow.focus();
    //保存写入内容
    var newContent = "";
    newContent += document.getElementById("print-content").outerHTML;
    newContent += "</body></html>";
    // 将HTML代码写入新窗口中
    newWindow.document.write(newContent);
    newWindow.print();
    // close layout stream
    newWindow.document.close();
    //关闭打开的临时窗口
    newWindow.close();
    return false;
};